package com.migu.rbac.controller;

import com.migu.rbac.model.SysMenu;
import com.migu.rbac.repository.SysMenuRepository;
import com.migu.rbac.service.SysMenuBackApiService;
import com.migu.rbac.service.SysMenuService;
import com.migu.rbac.vo.GlobalConst;
import com.migu.rbac.vo.MenuRVo;
import com.migu.rbac.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description TODO : 菜单
 * @Author: zj
 * @Date: 16:00 2019/4/2
 * @Version 1.0
 **/
@RestController
@RequestMapping(path = "/api/v1/menu")
public class MenuController {

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysMenuBackApiService saveMenuApi;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SysMenuRepository menuRepository;

    //添加菜单
    @PostMapping(value = "/addMenu")
    public Response addMenu(@RequestBody SysMenu sysMenu) {
        Response result = null;
        try {
            if (sysMenuService.savaMenu(sysMenu)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "菜单已经存在或添加失败，请联系管理员！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //修改菜单
    @PutMapping(value = "/updateMenu")
    public Response updateMenu(@RequestBody SysMenu sysMenu) {
        Response result = null;
        try {
            if (sysMenuService.updateMenu(sysMenu)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "修改失败或菜单已经存在!");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //删除菜单
    @DeleteMapping(value = "/deleteMenu/{menuid}")
    public Response deleteMenu(@PathVariable int menuid) {
        Response result = null;
        try {
            if (sysMenuService.deleteMenu(menuid)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "删除失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //分配菜单对应url
    @PostMapping(value = "/saveMenuApi", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response savaMenuApi(@RequestParam("menuid") int menuid, @RequestBody Integer[] apis) {
        Response result = null;
        try {
            if (saveMenuApi.saveMenuApi(menuid, apis)) {
                result = Response.success();
            } else {
                result = Response.error(GlobalConst.Code.ERROR_400, "分配失败！");
            }
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //查询菜单下所有的url id
    @GetMapping("/getUrlByMenuId/{menuid}")
    public Response getUrlByMenuId(@PathVariable int menuid) {
        Response result = null;
        try {
            result = Response.success(saveMenuApi.selectApiByMenuId(menuid));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //获取该用户拥有的菜单权限
    @GetMapping("/getMenuListByUserId/{userId}")
    public Response getMenuList(@PathVariable(value = "userId") String userId) {
        Response result = null;
        try {
            result = Response.success(sysMenuService.getMenuMap(userId));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //获取所有菜单权限
    @GetMapping("/getAllMenu")
    public Response getAllMenu() {
        Response result = null;
        try {
            result = Response.success(sysMenuService.findAllMenuList());
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //移动菜单
    @PostMapping("/moveMenu")
    public Response moveMenu(@RequestBody MenuRVo vo) {
        Response result = null;
        try {
            result = Response.success(sysMenuService.moveMenu(vo));
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    //修改菜单
    @PostMapping("/insertMenuPosition")
    public Response insertMenuPosition(@RequestBody SysMenu vo) {
        Integer integer = menuRepository.moveMenuPosition(vo);
        return Response.success(integer);
    }

    //所有菜单
    @GetMapping("/selectAll")
    public List<SysMenu> selectAll() {
        return menuRepository.selectAll();
    }

}
